package org.flashbrighton.as3bots.views
{	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	import net.hires.utils.*;
	import org.flashbrighton.as3bots.effects.Explosion;
	import org.flashbrighton.as3bots.events.*;
	import org.flashbrighton.as3bots.models.*;
	import org.flashbrighton.as3bots.views.ui.*;
	import org.flashbrighton.as3bots.views.ui.scores.ScoreBoard;
	

	public class GameView extends Sprite
	{
		public static const SCALE_FACTOR:Number = 50; //pixels per metre
		
		public var alert:Alert;
		
		private var _model:GameModel;
		private var _arena:ArenaView;
		private var _floor:Sprite;
		private var _explosions:Sprite;
		private var _botViewsByModel:Dictionary;
		
		
		public function GameView(model:GameModel)
		{
			_model = model;
			init();			
		}
		
		
		private function init():void
		{
			_botViewsByModel = new Dictionary(true);
			_model.addEventListener(DeadBotEvent.DEAD, removeBot);
		}
		
		
		public function addArenaView():void
		{					
			_arena = new ArenaView();
			_floor = new Sprite();
			_explosions = new Sprite();
			
			_arena.y = 30;
			_floor.y = 30;
			_explosions.y = 30;
		
			addChild(_floor);
			addChild(_arena);
			addChild(_explosions);
			
			setDebugDraw();
		}
		
		
		public function addBotView(botView:Sprite, botModel:BotModel):void
		{
			_botViewsByModel[botModel] = botView;
			_arena.addChild(botView);
		}
		
		
		public function update(e:Event=null):void
		{
			var botModel:BotModel;
			var botView:Sprite;
			
			// Go through body list and update sprite positions/rotations			
			for each (botModel in _model.botList)
			{
				botView = _botViewsByModel[botModel];
				botView.x = botModel.body.GetPosition().x * SCALE_FACTOR;
				botView.y = botModel.body.GetPosition().y * SCALE_FACTOR;
				botView.rotation = botModel.body.GetAngle() * 180 / Math.PI;
			}
			
			_model.physics.drawWireframes();
		}
		
		
		public function setUI():void
		{
			var header:TextField = createHeader("FLASHBRIGHTON ROBOT WARS III");
			header.x = 30;
			addChild(header);
			
			var scores:ScoreBoard = new ScoreBoard(_model)
			scores.x = 650;
			addChild(scores);
			
			var message:TextField = Message.create();
			message.x = 500;
			message.y = 50;
			addChild(message);
			
			var countdown:Countdown = new Countdown(_model);
			countdown.x = 540;
			addChild(countdown);
			
			alert = new Alert();
			alert.x = stage.stageWidth / 2;
			alert.y = stage.stageHeight / 2;
			addChild(alert);
			
			/*var stats:Stats = new Stats()
			stats.x = 700;
			addChild(stats);*/
		}
		
		
		public function removeBot(e:DeadBotEvent):void
		{
			_arena.removeChild(_botViewsByModel[e.botModel]);
			
			var explosion:Explosion = new Explosion(Math.random() * 50 + 50, e.botModel.bot.colour);
			explosion.x = e.botModel.body.GetPosition().x * SCALE_FACTOR;
			explosion.y = e.botModel.body.GetPosition().y * SCALE_FACTOR;		
			_explosions.addChild(explosion);
		}
		
		
		public function get explosions():Sprite 
		{ 
			return _explosions; 
		}
		
		
		private function createHeader(text:String):TextField
		{
			var tf:TextField = new TextField();
			tf.embedFonts = true;
			tf.defaultTextFormat = new TextFormat("Arkitech", 18, 0xFFFFFF);
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.selectable = false;
			tf.text = text;
			return tf;
		}
		
		
		private function setDebugDraw():void 
		{
			_model.physics.setDebugDraw(_arena);
		}
		
		
		
	}
}